<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="工单流水" prop="orderNo">
        <el-input
          v-model="queryParams.orderNo"
          placeholder="请输入平台流水号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="申诉流水" prop="sectionNo">
        <el-input
          v-model="queryParams.sectionNo"
          placeholder="请输入申诉信息流水号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>

      <!-- 添加附件状态选择框 -->
      <el-form-item label="附件状态" prop="fileStatus">
        <el-select
          v-model="queryParams.fileStatus"
          placeholder="请选择附件状态"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        >
          <el-option
            v-for="dict in appealStatusDictList"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="上传状态" prop="uploadStatus">
        <el-select
          v-model="queryParams.uploadStatus"
          placeholder="请选择上传状态"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        >
          <el-option
            v-for="dict in uploadStatusDictList"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="采集时间" prop="crawTime">
        <el-date-picker
          v-model="queryParams.crawTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>

      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          @click="openForm('create')"-->
<!--          v-hasPermi="['rpa:appeals-order:create']"-->
<!--        >-->
<!--          <Icon icon="ep:plus" class="mr-5px" /> 新增-->
<!--        </el-button>-->
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['rpa:appeals-order:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="平台流水号" align="center" prop="orderNo" />
      <el-table-column label="申诉流水号" align="center" prop="sectionNo" />
      <el-table-column label="地级市" align="center" prop="cityName" />
      <el-table-column label="主投企业" align="center" prop="entDeptName" />
      <el-table-column label="申诉时间" align="center" prop="appealTime" />
      <el-table-column label="附件数" align="center" prop="fileNum" />
      <el-table-column label="附件状态" align="center" prop="fileStatus">
        <template #default="scope">
          <el-tag v-if="scope.row.fileStatus === '待下载'"  type="info" effect="dark" class="mr-5px" >
            {{ scope.row.fileStatus }}
          </el-tag>
          <el-tag v-else-if="scope.row.fileStatus === '附件齐全'" type="success" effect="dark" class="mr-5px">
            {{ scope.row.fileStatus }}
          </el-tag>
          <el-tag v-else type="danger" effect="dark" class="mr-5px">
            {{ scope.row.fileStatus }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="上传状态" align="center" prop="uploadStatus">
        <!-- 状态分为三种情况，1、待上传，2、上传成功，3、上传失败，判断条件哦那个中文标识 -->
        <template #default="scope">
          <el-tag v-if="scope.row.uploadStatus === '待上传'"  type="info" effect="dark" class="mr-5px" >
            {{ scope.row.uploadStatus }}
          </el-tag>
          <el-tag v-else-if="scope.row.uploadStatus === '上传成功'" type="success" effect="dark" class="mr-5px">
            {{ scope.row.uploadStatus }}
          </el-tag>
          <el-tag v-else type="danger" effect="dark" class="mr-5px">
            {{ scope.row.uploadStatus }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="采集时间"
        align="center"
        prop="crawTime"
        :formatter="dateFormatter"
        width="180px"
      />
<!--      <el-table-column label="操作" align="center" min-width="120px">-->
<!--        <template #default="scope">-->
<!--          <el-button-->
<!--            link-->
<!--            type="primary"-->
<!--            @click="openForm('update', scope.row.id)"-->
<!--            v-hasPermi="['rpa:appeals-order:update']"-->
<!--          >-->
<!--            编辑-->
<!--          </el-button>-->
<!--          <el-button-->
<!--            link-->
<!--            type="danger"-->
<!--            @click="handleDelete(scope.row.id)"-->
<!--            v-hasPermi="['rpa:appeals-order:delete']"-->
<!--          >-->
<!--            删除-->
<!--          </el-button>-->
<!--        </template>-->
<!--      </el-table-column>-->
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
<!--  <AppealsOrderForm ref="formRef" @success="getList" />-->
</template>

<script setup lang="ts">
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { AppealsOrderApi, AppealsOrderVO } from './index.ts'
// import AppealsOrderForm from './AppealsOrderForm.vue'

/** 用户申诉工单 列表 */
defineOptions({ name: 'AppealsOrder' })

// 初始化附件状态字典列表，状态分为三种：待下载、附件齐全、附件缺失
const appealStatusDictList = ref([
  {
    dictValue: '待下载',
    dictLabel: '待下载',
  },
  {
    dictValue: '附件齐全',
    dictLabel: '附件齐全',
  },
  {
    dictValue: '附件缺失',
    dictLabel: '附件缺失',
  },
])

// 初始化上传状态字典列表，状态分为三种：待上传、上传成功、上传失败
const uploadStatusDictList = ref([
  {
    dictValue: '待上传',
    dictLabel: '待上传',
  },
  {
    dictValue: '上传成功',
    dictLabel: '上传成功',
  },
  {
    dictValue: '上传失败',
    dictLabel: '上传失败',
  },
])

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<AppealsOrderVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  orderNo: undefined,
  sectionNo: undefined,
  cityName: undefined,
  deptType: undefined,
  entDeptName: undefined,
  appealTime: [],
  appealContent: undefined,
  appealOrg: undefined,
  appealSource: undefined,
  serviceStatus: undefined,
  appealResult: undefined,
  acceptPerson: undefined,
  className1: undefined,
  className2: undefined,
  className3: undefined,
  serviceName1: undefined,
  serviceName2: undefined,
  serviceName3: undefined,
  createTime: [],
  ipAddress: undefined,
  isAppealFile: undefined,
  remark2: undefined,
  remark1: undefined,
  createName: undefined,
  isExcludeMust: undefined,
  fileStatus: undefined,
  uploadStatus: undefined,
  crawTime: [],
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await AppealsOrderApi.getAppealsOrderPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await AppealsOrderApi.deleteAppealsOrder(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await AppealsOrderApi.exportAppealsOrder(queryParams)
    download.excel(data, '用户申诉工单.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
